#include "stdio.h"
#include "DataAccessor.hpp"
#include "L2DataSet.hpp"

DataAccessor * data_set_get_service = nullptr;
DataAccessor * DataAccessor::getInstance(void){

    if( data_set_get_service == nullptr ){
        data_set_get_service = new DataAccessor();
        data_set_get_service->init();
    }
    return data_set_get_service;
}

/********************************** 方法实现 ***********************************/
bool DataAccessor::init(void){
    /* 创建信号量和互斥量 */
    data_set_get_semaphore = xSemaphoreCreateBinary();
    if( data_set_get_semaphore == NULL ) return false;
    data_set_get_speed_mutex = xSemaphoreCreateBinary();
    if( data_set_get_speed_mutex == NULL ) return false;
    xSemaphoreGive(data_set_get_speed_mutex);
    this->add("compressor.speedActual",0.0);
    return true;
}

bool DataAccessor::deinit(void){
    return true;
}

/************************************ 方法实现 *************************************/

bool DataAccessor::add(const char *label, bool value) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.add(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::add(const char *label, double value) {

    while( xSemaphoreTake(this->data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.add(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
        break;
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::add(const char *label, const char *value) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.add(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::remove(const char *label) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.remove(label);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::set(const char *label, bool value) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.set(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::set(const char *label, double value) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.set(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::set(const char *label, const char *value) {
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        dataset.set(label,value);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return true;
}

bool DataAccessor::getBoolean(const char *label) {
    bool get_data = false;
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        get_data = dataset.getBoolean(label);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return get_data;
}

double DataAccessor::getNumber(const char *label) {
    double get_data = 0.0;
    while( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        get_data = dataset.getNumber(label);
        xSemaphoreGive(data_set_get_speed_mutex);
        break;
    }
    xSemaphoreGive(data_set_get_semaphore);
    return get_data;
}

const char* DataAccessor::getString(const char *label) {
    char * get_data = nullptr;
    if( xSemaphoreTake(data_set_get_speed_mutex , portMAX_DELAY) == pdTRUE ){
        get_data = (char *)dataset.getString(label);
        xSemaphoreGive(data_set_get_speed_mutex);
    }
    xSemaphoreGive(data_set_get_semaphore);
    return get_data;
}